package com.example.saifly.inPagePlan.dailyQuestion;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.RecordModel.DailyQuestionnaire;

import java.io.IOException;
import java.util.Date;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class DailyQuestionActivity extends Activity implements RadioGroup.OnCheckedChangeListener,
        View.OnClickListener
{
    RadioGroup rgSleep,rgBreathe,rgChest,rgExpectoration;
    RadioGroup rgCough,rgEnergy,rgInfluence,rgGoout;

    RadioButton rbSleep1,rbSleep2,rbSleep3,rbSleep4,rbSleep5;
    RadioButton rbBreathe1,rbBreathe2,rbBreathe3,rbBreathe4,rbBreathe5;
    RadioButton rbChest1,rbChest2,rbChest3,rbChest4,rbChest5;
    RadioButton rbExpectoration1,rbExpectoration2,rbExpectoration3,rbExpectoration4,rbExpectoration5;
    RadioButton rbCough1,rbCough2,rbCough3,rbCough4,rbCough5;
    RadioButton rbEnergy1,rbEnergy2,rbEnergy3,rbEnergy4,rbEnergy5;
    RadioButton rbInfluence1,rbInfluence2,rbInfluence3,rbInfluence4,rbInfluence5;
    RadioButton rbGoout1,rbGoout2,rbGoout3,rbGoout4,rbGoout5;

    Button btSubmit,btExit;

    int sleepScore,breatheScore,energyScore,chestScore,gooutSCore,influenceScore;
    int coughScore,expectorationScore;
    int totalScore;
    private SharedPreferences sharedPreferences;
    private DailyQuestionnaire dailyQuestionnaire =new DailyQuestionnaire();
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_daily_question;
    }

    @Override
    protected void init() {
        super.init();
        InitView();

    }

    /**
     * 创建日期：2019/12/5 8:22
     * @author Yuan Li Xiang
     * @version 1.0
     * 函数说明： 找到组件加数值初始化
     */
    private void InitView()
    {
        sleepScore=0;
         energyScore=0;
        coughScore=0;
        chestScore=0;
        expectorationScore=0;
        gooutSCore=0;
        breatheScore=0;
        influenceScore=0;
        totalScore=0;
        sharedPreferences= MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

        rgBreathe=findViewById(R.id.daily_question_breathe_rg);
        rgChest=findViewById(R.id.daily_question_chest_rg);
        rgSleep=findViewById(R.id.daily_question_sleep_rg);
        rgEnergy=findViewById(R.id.daily_question_energy_rg);
        rgGoout=findViewById(R.id.daily_question_goout_rg);
        rgCough=findViewById(R.id.daily_question_cough_rg);
        rgInfluence=findViewById(R.id.daily_question_influence_rg);
        rgExpectoration=findViewById(R.id.daily_question_expectoration_rg);

        rbBreathe1=findViewById(R.id.daily_question_breathe_rb_1);
        rbBreathe2=findViewById(R.id.daily_question_breathe_rb_2);
        rbBreathe3=findViewById(R.id.daily_question_breathe_rb_3);
        rbBreathe4=findViewById(R.id.daily_question_breathe_rb_4);
        rbBreathe5=findViewById(R.id.daily_question_breathe_rb_5);

        rbChest1=findViewById(R.id.daily_question_chest_rb_1);
        rbChest2=findViewById(R.id.daily_question_chest_rb_2);
        rbChest3=findViewById(R.id.daily_question_chest_rb_3);
        rbChest4=findViewById(R.id.daily_question_chest_rb_4);
        rbChest5=findViewById(R.id.daily_question_chest_rb_5);

        rbSleep1=findViewById(R.id.daily_question_sleep_rb_1);
        rbSleep2=findViewById(R.id.daily_question_sleep_rb_2);
        rbSleep3=findViewById(R.id.daily_question_sleep_rb_3);
        rbSleep4=findViewById(R.id.daily_question_sleep_rb_4);
        rbSleep5=findViewById(R.id.daily_question_sleep_rb_5);

        rbCough1=findViewById(R.id.daily_question_cough_rb_1);
        rbCough2=findViewById(R.id.daily_question_cough_rb_2);
        rbCough3=findViewById(R.id.daily_question_cough_rb_3);
        rbCough4=findViewById(R.id.daily_question_cough_rb_4);
        rbCough5=findViewById(R.id.daily_question_cough_rb_5);

        rbGoout1=findViewById(R.id.daily_question_goout_rb_1);
        rbGoout2=findViewById(R.id.daily_question_goout_rb_2);
        rbGoout3=findViewById(R.id.daily_question_goout_rb_3);
        rbGoout4=findViewById(R.id.daily_question_goout_rb_4);
        rbGoout5=findViewById(R.id.daily_question_goout_rb_5);

        rbEnergy1=findViewById(R.id.daily_question_energy_rb_1);
        rbEnergy2=findViewById(R.id.daily_question_energy_rb_2);
        rbEnergy3=findViewById(R.id.daily_question_energy_rb_3);
        rbEnergy4=findViewById(R.id.daily_question_energy_rb_4);
        rbEnergy5=findViewById(R.id.daily_question_energy_rb_5);

        rbExpectoration1=findViewById(R.id.daily_question_expectoration_rb_1);
        rbExpectoration2=findViewById(R.id.daily_question_expectoration_rb_2);
        rbExpectoration3=findViewById(R.id.daily_question_expectoration_rb_3);
        rbExpectoration4=findViewById(R.id.daily_question_expectoration_rb_4);
        rbExpectoration5=findViewById(R.id.daily_question_expectoration_rb_5);

        rbInfluence1=findViewById(R.id.daily_question_influence_rb_1);
        rbInfluence2=findViewById(R.id.daily_question_influence_rb_2);
        rbInfluence3=findViewById(R.id.daily_question_influence_rb_3);
        rbInfluence4=findViewById(R.id.daily_question_influence_rb_4);
        rbInfluence5=findViewById(R.id.daily_question_influence_rb_5);

        btExit=findViewById(R.id.daily_question_btn_exit);
        btSubmit=findViewById(R.id.daily_question_btn_submit);


        btSubmit.setOnClickListener(this);
        btExit.setOnClickListener(this);
        rgExpectoration.setOnCheckedChangeListener(this);
        rgInfluence.setOnCheckedChangeListener(this);
        rgCough.setOnCheckedChangeListener(this);
        rgEnergy.setOnCheckedChangeListener(this);
        rgSleep.setOnCheckedChangeListener(this);
        rgChest.setOnCheckedChangeListener(this);
        rgBreathe.setOnCheckedChangeListener(this);
        rgGoout.setOnCheckedChangeListener(this);
    }
    /**
     * 创建日期：2019/12/16 18:47
     * @author Yuan Li Xiang
     * @version 1.0
     * 类说明： 将问卷结果保存到数据库的接口；
     */
    private interface ApiDailyQuestionData
    {
        @Headers({"Content-Type:application/json"})
        @POST("/plan/submitDailyQuestionnaire")
        Call<ResponseBody> DailyQuestionSetData(@Body JSONObject jsonObject);
    }

    private void DailyQuestionSetData(DailyQuestionnaire dailyQuestionnaire)
    {
        RetrofitHelper retrofitHelper=new RetrofitHelper();
        Retrofit retrofit=retrofitHelper.getRetrofit();

        ApiDailyQuestionData apiDailyQuestionData=retrofit.create(ApiDailyQuestionData.class);

        JSONObject jsonObject=new JSONObject();
        String jsonString = JSON.toJSONString(dailyQuestionnaire);
        jsonObject.put("dailyQuestionnaire",jsonString);
        Log.d("writedailyquestion", "DailyQuestionSetData: "+jsonObject.toString());

        Call<ResponseBody> call=apiDailyQuestionData.DailyQuestionSetData(jsonObject);
        final Response<ResponseBody> response;
        try {
            response = call.execute();
            if(response.body() != null){
                JSONObject result=JSON.parseObject(response.body().string());
                if(result.getInteger("code")!=null&&result.getInteger("code")==1){
                    //这里放正常流程操作
                    //  dailyQesFinish = (int) result.get("result");
                }
                else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DailyQuestionActivity.this, "网络出现异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(DailyQuestionActivity.this, "服务器在开小差儿", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.daily_question_btn_submit){
            if(sleepScore==0||energyScore==0||gooutSCore==0||breatheScore==0||expectorationScore==0
                    ||influenceScore==0||chestScore==0||coughScore==0) {
                Toast.makeText(this, "您有未填写的项目！", Toast.LENGTH_SHORT).show();
            } else{
                totalScore=energyScore+expectorationScore+breatheScore+coughScore+
                        chestScore+gooutSCore +influenceScore+sleepScore;

                SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
                String phoneNum=preferences.getString("uid","default");
                dailyQuestionnaire.setPatientPhone(phoneNum);

                Date date = new Date();

                dailyQuestionnaire.setSleep(sleepScore);
                dailyQuestionnaire.setEnergy(energyScore);
                dailyQuestionnaire.setExpectoration(expectorationScore);
                dailyQuestionnaire.setGoout(gooutSCore);
                dailyQuestionnaire.setBreathe(breatheScore);
                dailyQuestionnaire.setInfluence(influenceScore);
                dailyQuestionnaire.setCough(coughScore);
                dailyQuestionnaire.setChest(chestScore);
                dailyQuestionnaire.setDate(date);

                dailyQuestionnaire.setTotalscore(totalScore);
                //上传到数据库
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        DailyQuestionSetData(dailyQuestionnaire);
                    }
                }).start();

                if(totalScore<=10)
                {
                    Toast.makeText(this, "慢阻肺对您的生活有非常严重影响", Toast.LENGTH_LONG).show();
                }
                else if(totalScore>10&&totalScore<21)
                {
                    Toast.makeText(this, "慢阻肺对您的生活有严重影响", Toast.LENGTH_LONG).show();
                }
                else if(totalScore>30)
                {
                    Toast.makeText(this, "慢阻肺对您的生活没有影响", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(this,"慢阻肺对您的生活有一定影响",Toast.LENGTH_LONG).show();
                }

                this.finish();
            }

        }
        else if(v.getId()==R.id.daily_question_btn_exit){
            this.finish();
        }
    }

    //判断你选了那一项
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId)
        {
            case R.id.daily_question_sleep_rb_1:
                sleepScore = 1;
                //Toast.makeText(this,"你选择了"+rb_gender1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_sleep_rb_2:
                sleepScore = 2;
                // Toast.makeText(this,"你选择了"+rb_gender2.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_sleep_rb_3:
                sleepScore = 3;
                // Toast.makeText(this,"你选择了"+rb_department1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_sleep_rb_4:
                sleepScore = 4;
                //Toast.makeText(this,"你选择了"+rb_department2.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_sleep_rb_5:
                sleepScore = 5;
                //Toast.makeText(this,"你选择了"+rb_department3.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_cough_rb_1:
                coughScore = 1;
                //Toast.makeText(this,"你选择了"+rb_department4.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_cough_rb_2:
                coughScore = 2;
                //Toast.makeText(this,"你选择了"+rb_consume1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_cough_rb_3:
                coughScore = 3;
                //Toast.makeText(this,"你选择了"+rb_consume1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_cough_rb_4:
                coughScore = 4;
                //Toast.makeText(this,"你选择了"+rb_consume1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_cough_rb_5:
                coughScore = 5;
                //Toast.makeText(this,"你选择了"+rb_consume1.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_influence_rb_1:
                influenceScore = 1;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_influence_rb_2:
                influenceScore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_influence_rb_3:
                influenceScore = 3;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_influence_rb_4:
                influenceScore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_influence_rb_5:
                influenceScore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_expectoration_rb_1:
                expectorationScore = 1;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_expectoration_rb_2:
                expectorationScore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_expectoration_rb_3:
                expectorationScore = 3;
                break;

            case R.id.daily_question_expectoration_rb_4:
                expectorationScore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_expectoration_rb_5:
                expectorationScore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_goout_rb_1:
                gooutSCore = 1;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_goout_rb_2:
                gooutSCore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_goout_rb_3:
                gooutSCore = 3;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_goout_rb_4:
                gooutSCore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_goout_rb_5:
                gooutSCore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;

            case R.id.daily_question_energy_rb_1:
                energyScore = 1;
                break;
            case R.id.daily_question_energy_rb_2:
                energyScore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_energy_rb_3:
                energyScore = 3;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_energy_rb_4:
                energyScore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_energy_rb_5:
                energyScore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_breathe_rb_1:
                breatheScore = 1;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_breathe_rb_2:
                breatheScore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_breathe_rb_3:
                breatheScore = 3;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_breathe_rb_4:
                breatheScore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_breathe_rb_5:
                breatheScore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_chest_rb_1:
                chestScore = 1;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_chest_rb_2:
                chestScore = 2;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_chest_rb_3:
                chestScore = 3;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_chest_rb_4:
                chestScore = 4;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
            case R.id.daily_question_chest_rb_5:
                chestScore = 5;
                // Toast.makeText(this,"你选择了"+rb_grade1.getText().toString(),Toast.LENGTH_LONG).show();
                break;
        }
    }

}
